<?php
    class Utility {
    	/**
		 * sendNoCacheHeaders
		 *
		 * Make sure the browser doesn't cache our
		 * dynamic content
		 */
		static function sendNoCacheHeaders()
		{
			# 'Expires' in the past
			header("Expires: Mon, 1 Jan 1990 00:00:00 GMT");
			
			# Always modified
			header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
			
			# HTTP/1.1
			header("Cache-Control: no-store, no-cache, must-revalidate");
			header("Cache-Control: post-check=0, pre-check=0", false);
			
			# HTTP/1.0
			header("Pragma: no-cache");
		}
		
		/**
		 * Dynamically load all the libraries in the lib dir
		 */
		static function loadLibs($dir)
		{
			foreach ( scandir($dir) as $file ){
				if ( is_file($dir.$file) ) require_once($dir.$file);
			}
		}
		
		static function arrayExtract($array, $key){
			$newArray = array();
			foreach ( $array as $item ){
				if ( isset($item[$key]) ) $newArray[] = $item[$key];
			}
			return $newArray;
		}
		
		static function debug($obj){
			if ( Config::$debug ) HTML::dump($obj);
		}
		
		static function setFlash($message){
			$_SESSION['message'] = $message;
		}
		
		static function getFlash($unset=true){
			if ( ! isset($_SESSION['message']) ) return false;
			$message = $_SESSION['message'];
			if ( $unset ) unset($_SESSION['message']);
			return $message;
		}
		
		static function parseURL($url){
			$parts = split('/', $url);
			//if ( $parts[0] != '' and ! isset(Config::$controllers[$parts[0]]) ) HTTP::notFound();
			$classname	= $parts[0] != '' ? $parts[0] : Config::$defaultRoute;
			$function	= isset($parts[1]) && $parts[1] != '' ? $parts[1] : 'list';
			$id			= isset($parts[2]) ? $parts[2] : '';
			return array($classname, $function, $id);
		}
		
		/**
		 * Our "engine" function.
		 * Translates the URL triplet into a method on one of
		 * our controllers, do error checking and finally run
		 * the method. TODO: tidy up function logic
		 * 
		 * @return Boolean True on success, false on failure
		 * @param $classname String Name of class to lookup
		 * @param $function String Name of the method to call
		 * @param $id String ID of object to run method on. If ID is blank, assume a class method
		 */
		static function runController($classname, $function, $id){
			if ( $id == '' ){
				$method = array($classname, $function);
			} else {
				if (! is_callable(array($classname, 'find')) ){
					// Load an error page
					HTTP::error("Can't find any $classname");
					return;
				}
				$resource = call_user_func(array($classname, 'find'), $id);
				if (! is_a($resource, $classname)){
					// Load an error page
					HTTP::notFound();
					return;
				}
				$method = array(&$resource, $function);
			}
			if (is_callable($method)){
				call_user_func($method);
			} else {
				// Load an error page
				HTTP::notFound();
			}			
		}
    }
	
	/**
	 * Provide some useful methods to our controllers
	 * 
	 */
	abstract class Controller {		
		/**
		 * Render a view for a controller
		 * 
		 * @param $filename String File containing view
		 * @param $pageTitle String HTML page title
		 */
		function loadView($filename, $pageTitle='', $params=array())
		{
			extract($params);
			$myFilename = "./app/views/".$filename.".php";
			// Parse $filename into $pageHTML
			if (! is_file($myFilename)) return false;
			// Suck $myFilename into our output buffer
			ob_start();
		    include($myFilename);
		    $pageHTML = ob_get_contents();
		    ob_end_clean();
			// Load wrapper page which includes the $pageHTML we just defined
			include(Config::$wrapperPage);
		}
	}
?>
